/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "userinfodialog.h"

#include <QDialog>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFrame>
#include <QPushButton>
#include <QLabel>
#include <QString>

#include "../Types/user.h"

#include "../utils.h"
#include "../globals.h"

UserInfoDialog::UserInfoDialog(const User& u, QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle(tr("NUP : User '") + u.name + QString(tr("' viewer")));
    this->setWindowIcon(QIcon(ICONSDIR + QString("user_green.png")));

    pMainGrid = new QVBoxLayout(this);

    pTitleLayout = new QHBoxLayout();
    pTitleLayout->setAlignment(Qt::AlignHCenter);

    pFlagLabel = new QLabel(this);
    pFlagLabel->setPixmap(QPixmap(Utils::getFlagPathFromName(u.flag)));
    pFlagLabel->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);

    pTitleLabel = new QLabel(QString("<b>") + u.name + QString("</b>"),this);

    if ( !u.realName.isEmpty() )
    {
        pRealNameLabel = new QLabel(tr("Name: ") + u.realName,this);
    }
    else
    {
        pRealNameLabel = NULL;
    }

    if ( !u.group.isEmpty() )
    {
        pGroupLabel = new QLabel(tr("Group: ") + u.group,this);
    }
    else
    {
        pGroupLabel = NULL;
    }

    if ( !u.website.isEmpty() )
    {
        pWebsiteLabel = new QLabel(QString("<a href=\"") + u.website.toString() + QString("\">") + u.website.toString() + QString("</a>"),this);
        pWebsiteLabel->setOpenExternalLinks(true);
    }
    else
    {
        pWebsiteLabel = NULL;
    }

    pHLine1 = new QFrame(this);
    pHLine1->setFrameStyle(QFrame::HLine);
    pHLine1->setLineWidth(1);

/*
    if ( !u.avatarLink.isEmpty() )
    {
        pAvatarLabel = new QLabel(QString("'><img src='") + u.avatarLink + QString("'>"),this);// + QString(tr("title=\"Avatar\" alt=\"Avatar (Broken link)\"/>")),this);
    }
    else
    {
*/
        pAvatarLabel = NULL;
    // }

    if ( !u.infoline.isEmpty() )
    {
        pInfolineLabel = new QLabel(QString("<i>")+ u.infoline +QString("</i>"),this);
    }
    else
    {
        pInfolineLabel = NULL;
    }

    if ( !u.twitterId.isEmpty() )
    {
        pTwitterLabel = new QLabel(QString(tr("Twitter: ")) + u.twitterId,this);
    }
    else
    {
        pTwitterLabel = NULL;
    }

    pHLine2 = new QFrame(this);
    pHLine2->setFrameStyle(QFrame::HLine);
    pHLine2->setLineWidth(1);

    pValidateButton = new QPushButton(tr("Ok"),this);

    pMainGrid->addLayout(pTitleLayout);
        pTitleLayout->addWidget(pFlagLabel);
        pTitleLayout->addWidget(pTitleLabel);
    if ( pRealNameLabel )
    {
        pMainGrid->addWidget(pRealNameLabel,0,Qt::AlignHCenter);
    }

    if ( pGroupLabel )
    {
        pMainGrid->addWidget(pGroupLabel,0,Qt::AlignHCenter);
    }

    if ( pWebsiteLabel )
    {
        pMainGrid->addWidget(pWebsiteLabel,0,Qt::AlignHCenter);
    }

    if ( pAvatarLabel || pInfolineLabel || pTwitterLabel )
    {
        pMainGrid->addWidget(pHLine1);
    }

    if ( pAvatarLabel )
    {
        pMainGrid->addWidget(pAvatarLabel,0,Qt::AlignHCenter);
    }

    if ( pInfolineLabel )
    {
        pMainGrid->addWidget(pInfolineLabel,0,Qt::AlignHCenter);
    }

    if ( pTwitterLabel )
    {
        pMainGrid->addWidget(pTwitterLabel,0,Qt::AlignHCenter);
    }

    pMainGrid->addWidget(pHLine2);
    pMainGrid->addWidget(pValidateButton);

    connect(pValidateButton, SIGNAL(clicked()), this, SLOT(accept()));

    qDebug() << "GroupInfoDialog created";
}

UserInfoDialog :: ~UserInfoDialog(void)
{
    qDebug() << "UserInfoDialog deleted";
}

void UserInfoDialog :: accept(void)
{
    qDebug() << "UserInfoDialog :: accept";

    this->done(QDialog::Accepted);
    emit deleteUIDSignal(this);
}

void UserInfoDialog :: reject(void)
{
    qDebug() << "UserInfoDialog :: reject";

    this->done(QDialog::Rejected);
    emit deleteUIDSignal(this);
}

